package GUI;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ImageIcon;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class LoginGUI {
    private String userName;
    private String passWord;
    private int flag = 0;
    JFrame inter = new JFrame("皇家购物系统");
    public LoginGUI()
    {
        interGUI();
    }
    public void interGUI() {
        inter.setResizable(false);
        inter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        inter.setLayout(new BorderLayout());
        ImageIcon icon = new ImageIcon("logo1.jpg"); //插入图片

        //
        //待加背景
        //

        //界面布局
        JPanel panel0 = new JPanel(new BorderLayout());
        JLabel photoLabel = new JLabel(icon);
        panel0.add(photoLabel,BorderLayout.NORTH);
        inter.add(panel0,BorderLayout.NORTH);

        JComboBox<String> cmb = new JComboBox<>();
        cmb.addItem("--请选择--");
        cmb.addItem("--商家系统--");
        cmb.addItem("--用户系统--");
        panel0.add(cmb,BorderLayout.CENTER);


        cmb.setFont(new Font("宋体",Font.PLAIN,20));
        JPanel panel1=new JPanel(new GridLayout(2,2,0,20));
        JLabel userLabel = new JLabel("账  号：");
        JTextField userText = new JTextField(10);
        userLabel.setFont(new Font("宋体",Font.BOLD,20));
        userText.setFont(new Font("宋体",Font.PLAIN,20));

        JLabel passwordLabel = new JLabel("密  码：");
        JPasswordField passwordField = new JPasswordField(10);
        passwordField.setEchoChar('*');
        passwordLabel.setFont(new Font("宋体",Font.BOLD,20));
        passwordField.setFont(new Font("宋体",Font.PLAIN,20));



        panel1.add(userLabel);
        panel1.add(userText);
        panel1.add(passwordLabel);
        panel1.add(passwordField);
        inter.add(panel1,BorderLayout.CENTER);

        JPanel panel2 = new JPanel(new FlowLayout());
        JButton button = new JButton("登录");
        button.setFont(new Font("宋体",Font.PLAIN,20));
        button.setSize(200,10);
        panel2.add(button);
        inter.add(panel2,BorderLayout.SOUTH);


        //下拉菜单事件触发器里面嵌套按钮触发器
        cmb.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                // TODO Auto-generated method stub
                //cmb.getSelectedIndex()  ==1 为商家系统  ==2 为买家系统

                if (e.getStateChange() == ItemEvent.SELECTED) {

                    //按钮触发器
                    button.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            // TODO Auto-generated method stub
                            userName = userText.getText();
                            passWord = passwordField.getText();
                            if(cmb.getSelectedIndex()==1){
                                flag = 1;

                            }
                            else if(cmb.getSelectedIndex()==2) {
                                flag = 2;

                            }
                        }
                    });
                }

            }
        });

        inter.pack();
        inter.setVisible(true);
        inter.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width)/2-250, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-300, 300,350);

    }

    public String getUserName() {
        return userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public int getFlag() {
        return flag;
    }

    public void offInter(){ inter.setVisible(false);}

}
